package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

/*
* 体检机构地址
* 0*/
@RestController
@RequestMapping("/address")
public class AddressController_lzl {

    @Reference
    private AddressService addressService;
    @Autowired
    private JedisPool jedisPool;
    /*
     * 图片上传
     * */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){

        try {
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);
            //七牛云存储
            String filename = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);
            //存reids
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,filename);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

    }
    /*
     * 根据条件分页查询
     * */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult =  addressService.findPage(queryPageBean);
        return pageResult;
    }

    /*
     * 新增
     * */
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        try {
            addressService.add(address);
            return new Result(true, "添加体检机构成功！");
        } catch (Exception e) {
            return new Result(false,"添加体检机构失败！");
        }
    }

    /*
     * 根据id查询address对象回显
     * */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Address address = addressService.findByid(id);
            return new Result(true,"查询体检机构成功！",address);
        } catch (Exception e) {
            return new Result(false,"查询体检机构失败！");
        }
    }
    /*
     * 修改体检机构中心
     * */
    @RequestMapping("/update")
    public Result update(@RequestBody Address address){
        try {
            addressService.update(address);
            return new Result(true,"编辑体检机构成功");
        } catch (Exception e) {
            return new Result(false,"编辑体检机构失败");
        }
    }
    /*
     * 删除单条记录体检机构
     * */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            addressService.delete(id);
            return new Result(true,"删除体检机构成功！");
        } catch (RuntimeException e) {
            return new Result(false,"删除体检机构失败！");
        }

    }
}
